package com.hfjyz.registration.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.registration.command.RoomEditCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.service.RoomService;

public class RoomEditController extends SimpleFormController {

	private RoomService roomService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#
	 * referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object,
	 * org.springframework.validation.Errors, int)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		RoomEditCommand cmd = (RoomEditCommand) command;
        Exam exam = (Exam) request.getSession().getAttribute(ExamContants.SESSION_KEY_EXAM_INFO);
        cmd.setExam(exam);
		String id = request.getParameter("roomId");
		cmd.setId(id);
		return roomService.initEdit(cmd, errors);
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		RoomEditCommand cmd = (RoomEditCommand) command;
		Exam exam = (Exam) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_EXAM_INFO);
		cmd.setExam(exam);
		roomService.editOk(request, cmd, errors);
		if (errors.hasErrors()) {
			return new ModelAndView(getFormView(), errors.getModel());
		}
		return new ModelAndView(new RedirectView("roomList.action"),
				errors.getModel());
	}

	/**
	 * @return the roomService
	 */
	public RoomService getRoomService() {
		return roomService;
	}

	/**
	 * @param roomService
	 *            the roomService to set
	 */
	public void setRoomService(RoomService roomService) {
		this.roomService = roomService;
	}

}